package com.assignwork.service.impl;

import com.assignwork.dto.VoyageWorkNodeSearchDTO;
import com.assignwork.dto.VoyageWorkNodeSubmitDTO;
import com.assignwork.entity.VoyageWorkNode;
import com.assignwork.mapper.VoyageWorkNodeMapper;
import com.assignwork.service.IVoyageWorkNodeService;
import com.assignwork.vo.VoyageWorkNodeLastData;
import com.assignwork.vo.VoyageWorkNodePageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.List;

/**
 * 船舶停时实现类
 */
@Service
@AllArgsConstructor
public class VoyageWorkNodeServiceImpl extends BaseServiceImpl<VoyageWorkNodeMapper, VoyageWorkNode> implements IVoyageWorkNodeService {

    @Override
    public IPage<VoyageWorkNodePageVO> selectVoyageWorkNodePage(VoyageWorkNodeSearchDTO dto, IPage<VoyageWorkNodePageVO> page) {
        List<VoyageWorkNodePageVO> data = baseMapper.selectVoyageWorkNodeList(dto, page);
        for (VoyageWorkNodePageVO vo : data) {
            if (!Func.isNull(vo.getNodeCode())) {
                vo.setNodeName(DictBizCache.getValue("work_node_code", vo.getNodeCode()));
            }
            if (!Func.isNull(vo.getBerthKey())) {
                vo.setBerthName(DictBizCache.getValue("berth_code", vo.getBerthKey()));
            }
        }
        return page.setRecords(data);
    }

    @Override
    public Boolean submitVoyageWorkNode(VoyageWorkNodeSubmitDTO dto) {
        if (!Func.isNull(dto.getBeginTime()) && !Func.isNull(dto.getEndTime())) {
            Duration duration = Duration.between(dto.getBeginTime(), dto.getEndTime());
            dto.setElapsedTime(new BigDecimal(duration.toMinutes()));
        } else {
            dto.setElapsedTime(new BigDecimal(0));
        }
        VoyageWorkNode entity = BeanUtil.copy(dto, VoyageWorkNode.class);
        return super.saveOrUpdate(entity);
    }

    @Override
    public VoyageWorkNodeLastData selectLastNodeData(VoyageWorkNodeSearchDTO dto) {
        return baseMapper.selectLastNodeData(dto);
    }
}